package cn.njupt;

import cn.njupt.list.LinkedList;
import cn.njupt.set.Set;

public class ListSet<E> implements Set<E> {

    private LinkedList<E> list=new LinkedList<>();
    @Override
    public int size() {
        return list.size();
    }

    @Override
    public boolean isEmpty() {
        return list.isEmpty();
    }

    @Override
    public void clear() {
        list.clear();
    }

    @Override
    public boolean contains(E element) {
        return list.contains(element);
    }

    @Override
    public void add(E element) {
        int index = list.indexOf(element);
        if(index!=list.ELEMENT_NOT_FOUND){
            //证明存在element
            list.set(index,element);
        }else{
            list.add(element);
        }
    }

    @Override
    public void remove(E element) {
        int index = list.indexOf(element);
        if(index!=list.ELEMENT_NOT_FOUND){
            list.remove(index);
        }
    }

    @Override
    public void traversal(Visitor<E> visitor) {
        if(visitor==null)return;
        int size= list.size();
        for (int i = 0; i < size; i++) {
            if(visitor.visit(list.get(i))){
                return;
            }
        }
    }
}
